package mpbe.dialogos;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

import mpbe.Componentes.TableColEdit.ColumnTCE;
import mpbe.Componentes.TableColEdit.ModifyCellListener;
import mpbe.Componentes.TableColEdit.TableCE;
import mpbe.Componentes.TableColEdit.WgBooleanCE;
import mpbe.Componentes.TableColEdit.WgTextCE;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

public class TCEInputDialog extends Dialog {
	private String message;

	private String [] designaciones;
	private String [] res;
	
	private Table table = null;
	private TableCE TCE = null;
	private ColumnTCE colDescripcion = null;
	private ColumnTCE colValor = null;
	

	public TCEInputDialog(Shell parent) {
		this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
	}

	public TCEInputDialog(Shell parent, int style) {
		super(parent, style);
		setText("Input Dialog");
		setMessage("Please enter a value:");
		
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String [] getDesignaciones() {
		return designaciones;
	}

	public void setDesignaciones(String [] txtDesig) {
		this.designaciones = txtDesig;
		res = new String [designaciones.length];
		for (int ii=0; ii<res.length; ii++) res[ii] = "";
	}
	
	public boolean setDefault(String [] txtDefault) {
		// Asigna valores por defecto a al resultado
		if (txtDefault.length==res.length) {
			res = txtDefault;
			return true;
		}
		else return false;
	}
	
	public String [] open() {
		Shell shell = new Shell(getParent(), getStyle());
		shell.setText(getText());
		createContents(shell);
		shell.pack();
		
		Display display = getParent().getDisplay();
		
		// Move the dialog to the center of the top level shell.
	    Rectangle shellBounds = display.getBounds();
	    Point dialogSize = shell.getSize();
	    shell.setLocation(
	      shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
	      shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
	    
	    shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return res;
	}

	private void createContents(final Shell shell) {
		shell.setLayout(new GridLayout(2, true));

		final Label label = new Label(shell, SWT.NONE);
		label.setText(message);
		GridData data = new GridData();
		data.horizontalSpan = 2;
		label.setLayoutData(data);

		table = new Table (shell, SWT.BORDER);
		GridData gdTable = new GridData();
		gdTable.horizontalAlignment = GridData.FILL;
		gdTable.grabExcessHorizontalSpace = true;
		gdTable.grabExcessVerticalSpace = true;
		gdTable.verticalAlignment = GridData.FILL;
		gdTable.horizontalSpan = 2;
		table.setLayoutData(gdTable);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TCE = new TableCE(table);
		// columna 1 boolean - Activa
		colDescripcion = new ColumnTCE(SWT.BORDER, "Descripcion", false, true, false);
		WgTextCE wgCol1 = new WgTextCE(SWT.NONE);
		colDescripcion.setWidget(wgCol1);
		TableColumn TC = TCE.addColumn(colDescripcion);
		TC.setWidth(200);
		
		// columna 2 Text - Designacion
		colValor = new ColumnTCE(SWT.BORDER, "Valor", false, true, true);
		WgTextCE wgCol2 = new WgTextCE(SWT.NONE);
		wgCol2.addModifyCellListener(new ModifyCellListener(){
			public void modifyCell(int row, Object o) {
				res[row] = (String) o;
			}
		});
		colValor.setWidget(wgCol2);
		TC = TCE.addColumn(colValor);
		TC.setWidth(80);
		// carga las columnas
		String [] st = new String [2];
		for (int ii = 0; ii < res.length; ii++) {
			st[0] = designaciones[ii];
			st[1] = res[ii];
			TCE.addRow(st);
		}

		Button ok = new Button(shell, SWT.PUSH);
		ok.setText("OK");
		data = new GridData(GridData.FILL_HORIZONTAL);
		ok.setLayoutData(data);
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				shell.close();
			}
		});

		Button cancel = new Button(shell, SWT.PUSH);
		cancel.setText("Cancel");
		data = new GridData(GridData.FILL_HORIZONTAL);
		cancel.setLayoutData(data);
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				res = null;
				shell.close();
			}
		});

		shell.setDefaultButton(ok);
	}
}
